package edu.zut.pioneer.commodity;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import edu.zut.pioneer.base.controller.GenericController;
import edu.zut.pioneer.commodity.domain.CommoditySort;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

@RestController
@RequestMapping(value="/commoditySort",produces = { MediaType.APPLICATION_JSON_VALUE})
public class CommoditySortController extends GenericController<CommoditySort,Long,CommoditySortManager> {
    CommoditySortManager commoditySortManager;
    @Resource
    public void setCommodityManager(CommodityManager commodityManager){
        this.commoditySortManager = commoditySortManager;
        this.manager = this.commoditySortManager;

    }
    @ResponseBody
    @RequestMapping("/findAll/{page}/{pageSize}")
    public String findAllProductCategory(@PathVariable("page") int page, @PathVariable("pageSize") int pageSize){
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        Pageable pageable = PageRequest.of(page,pageSize, sort);
        Page<CommoditySort> list =commoditySortManager.findAll(pageable);
        int number = list.getNumber();
        HashMap<String,Object> map = new HashMap<>();
        map.put("number",number);
        map.put("userList",list);
        //map集合转化为JSON字符串进行数据传输
        String res = JSON.toJSONString(map);
        return res;
    }

}
